import { mapMutations, mapGetters, mapActions } from 'vuex'
import * as types from '@/store/mutations-types'
// import { imgfrom } from '@/config/config'
// export const commonMixin = {
//   methods: {
//     // getImgSrc (remoteImgUrl) {
//     //   let articlePic = remoteImgUrl
//     //   if (!(articlePic.indexOf('http') > -1)) {
//     //     articlePic = 'http:' + articlePic
//     //   }
//     //   return imgfrom.url + '/apizdm/getimg?url=' + articlePic
//     // },
//     filterIds (newArr, oldArr) { // 保留newArr中，所有项目都与oldArr不同
//       return newArr.filter((id) => {
//         return !(oldArr.indexOf(id) > -1)
//       })
//     },
//     filterList (arrIds, arrItems) { // 保留arrItems，其中保留arrItems.article_id出现在arrIds中的才保留
//       return arrItems.filter((item) => {
//         return arrIds.indexOf(item.article_id) > -1
//       })
//     },
//     genArrOfIds (arr) {
//       let tmpArr = []
//       arr.forEach((item) => {
//         tmpArr.push(item.article_id)
//       })
//       return tmpArr
//     }
//   }
// }
export const searchTitleMixin = {
  methods: {
    ...mapMutations({
      setShowSearchTitle: types.SET_SEARCH_TITLE
    })
  },
  computed: {
    ...mapGetters({
      showSearchTitle: 'showSearchTitle'
    })
  }
}
// const LOADING_PLACEHODER_HEIGHT = 30

export const scrollMixin = {
  data () {
    return {
      scrollTimer: null,
      currentScrollT: 0
    }
  },
  methods: {
    resetMountedTimestamp () {
      this.mountedTimestamp = new Date().getTime()
    },
    scrollMethod () {
      clearTimeout(this.scrollTimer)
      this.scrollTimer = setTimeout(() => {
        let scrollT = document.documentElement.scrollTop || document.body.scrollTop
        let scrollH = document.documentElement.scrollHeight || document.body.scrollHeight
        let clientH = window.innerHeight // document.documentElement.clientHeight || document.body.clientHeight
        scrollT = Math.ceil(scrollT)
        this.currentScrollT = scrollT
        if (scrollT >= scrollH - clientH) {
          // console.log('到底部了, scrollT:', scrollT)
          window.scrollTo(0, scrollT + 1)
          this.getMoreList()
        } else if (scrollT === 0) {
          this.scrollGotTop()
          console.log('到顶部了')
        }
      }, 200)
    },
    getMoreList () {
      throw new Error('引用scrollMixin必须实现getMoreList方法')
    },
    scrollGotTop () {
      console.log('scrollMixin scrollGotTop')
    }
  },
  destroyed () {
    window.removeEventListener('scroll', this.scrollMethod)
  },
  activated () {
    window.scrollTo(0, this.currentScrollT || 0)
    window.addEventListener('scroll', this.scrollMethod)
  },
  deactivated () {
    // 登记 离开时的滚动条位置
    window.removeEventListener('scroll', this.scrollMethod)
  }
}

export const routeGoodsDetailMixn = {
  methods: {
    ...mapActions({
      // routeToGoodsDetailAction: 'routeToGoodsDetailAction'
    }),
    routeToGoodsDetailPage (goods) {
      if ([11, 6].indexOf((goods.article_channel_id || goods.channel_id)) > -1) {
        return
      }
      if (goods.article_id) {
        // console.log('routeToGoodsDetailPage', (goods.article_channel_id || goods.channel_id))
        // this.routeToGoodsDetailAction(goods)
        this.$store.commit('SET_GOODS_DETAIL_ITEM', goods)
        // console.log()
        if (this.$route.name !== 'goodsDetail') {
          this.$router.push('/goodsdetail/' + goods.article_id)
        }
      }
    }
  }
}
